package com.wondersgroup.fjzlpt.org.dao.impl.xymb;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;
import com.wondersgroup.fjzlpt.org.dao.inter.xymb.XymbDAO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by lion on 2017/7/4.
 */
@Repository
public class XymbDAOImpl extends BaseJTDAOImpl implements XymbDAO{

    @Override
    @Autowired
    @Qualifier("TB_XXWH_XYMB")
    public void setTb(TableBean tb) {
        super.setTb(tb);
    }

    /**
     * 查询协议服务
     * @param XYMBID
     * @return
     */
    public JSONArray queryXyfw(String XYMBID){
        StringBuilder sqlBuilder = new StringBuilder("select b.FWID, b.FWMC, b.FWNR from TB_XXWH_XYFWXX a, TB_XXWH_FWXX b where 1=1 \n")
                .append("and a.XYMBID=? and a.FWID=b.FWID");
        SQLBean sb = new SQLBean(sqlBuilder.toString(), new Object[]{XYMBID});
        return this.queryList(sb);
    }

    @Override
    public JSONObject queryJtqyXymbDefault() {
        StringBuffer sb = new StringBuffer(" select xymbid from tb_xxwh_xymb a where a.xymblx = '01' and a.zt = '1' ");
        SQLBean sqlBean = new SQLBean(sb.toString());
        return this.queryOne(sqlBean);
    }

    /**
     * 分页查询协议模板
     * @return
     */
    public JSONObject queryPageXymb(Map<String, String> map){
        StringBuilder sbud = new StringBuilder("select XYMBID, XYMBMC, XYMBLX, CJRQ, ZT from ")
                .append(this.tb.getTableName()).append(" where 1=1");
        List list = new ArrayList(2);
        String value = map.get("XYMBMC");
        if(!StringUtils.isEmpty(value)){
            sbud.append(" and XYMBMC like ?");
            list.add("%"+value+"%");
        }
        value = map.get("XYMBLX");
        if(!StringUtils.isEmpty(value)){
            sbud.append(" and XYMBLX = ?");
            list.add(value);
        }
        PageQuerySQLBean sb = checkAndCreatePageQuerySQLBean(map, sbud.toString(), "order by CJRQ desc", list.toArray());
        return super.queryPage(sb, ResultHandlerConstant.JSONARRAY);
    }

    /**
     * 家庭签约协议状态统一改为0
     * @param map
     * @return
     */
    public void updateJtqyxyZt(Map<String, String> map){
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append("update ").append(this.tb.getTableName());
        sqlBuf.append(" set ZT = 0 where XYMBLX = '01'"); //家庭签约协议：XYMBLX==01
        SQLBean bean = new SQLBean(sqlBuf.toString());
        super.update(bean);
    }

    /**
     * 根据条件查询一条数据
     *  --查询启用中的家庭签约协议信息
     * @param map
     * @return
     */
    public JSONObject queryOne(Map<String, String> map){
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append("select * from ").append(this.tb.getTableName()).append(" a \n");
        sqlBuf.append("where 1=1 ");
        List sqlParams = new ArrayList();
        //协议模板id
        String value = map.get("XYMBID");
        if(!StringUtils.isEmpty(value)){
            sqlBuf.append("and XYMBID = ? ");
            sqlParams.add(value);
        }
        //协议模板id
        value = map.get("XYMBLX");
        if(!StringUtils.isEmpty(value)){
            sqlBuf.append("and XYMBLX = ? ");
            sqlParams.add(value);
        }
        //协议模板状态
        value = map.get("ZT");
        if(!StringUtils.isEmpty(value)){
            sqlBuf.append("and ZT = ? ");
            sqlParams.add(value);
        }
        SQLBean bean = new SQLBean(sqlBuf.toString(),sqlParams.toArray());

        return super.queryOne(bean);
    }
}
